package game.module.sss.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import game.module.sss.bean.SssRoom;
import game.module.sss.logic.SssDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.C2SSssBackRoomHall;
import lua.LuaMessageSss.S2CSSSJoinRoom;
import lua.LuaMessageSss.S2CSssBackRoomHall;
import lua.LuaMessageSss.SSSMemberInfo;
@MsgCodeAnn(msgcode = C2SSssBackRoomHall.id , accessLimit = 500)
public class SssBackRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssBackRoomProcessor.class);
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("sss back hall room,playerId={}",playerId);
		//是否在房间中
		boolean isInRoom = SssDataManager.getInstance().isPlayerInRoom(playerId);	
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//房间是否存在
		SssRoom sssroom = SssDataManager.getInstance().getSssRoomByPlayerId(playerId);
		if(sssroom==null){
			S2ErrorCode respMsg = new S2ErrorCode(104);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//ret
		String room_id = sssroom.getUuid();
		S2CSssBackRoomHall respMsg = new S2CSssBackRoomHall();
		respMsg.hall_info = new S2CSSSJoinRoom();
		respMsg.hall_info.room_id = room_id;
		respMsg.hall_info.round_count = sssroom.getRoundCount();
		respMsg.hall_info.anti_cheet = sssroom.getPrevent_cheat();
		respMsg.hall_info.mapai = sssroom.getMapai();
		respMsg.hall_info.daqiang = sssroom.getDaqiang();
		respMsg.hall_info.sunzi_a = sssroom.getSunzi_a();
		respMsg.hall_info.baodao = sssroom.getBaodao();
		respMsg.hall_info.jindouNum = sssroom.getJindou();
		respMsg.hall_info.is_jindou = sssroom.getIs_jindou();
		respMsg.hall_info.roomFeeType = sssroom.getRoomFeeType();
		respMsg.hall_info.player_count = sssroom.getPlayer_count();
		respMsg.hall_info.add_color = sssroom.getAdd_color();
		respMsg.hall_info.members =new SSSMemberInfo[sssroom.getPlayerList().size()];
		List<Integer> playerIdList = sssroom.getPlayerList();
		int i =0;
		for(Integer aPlayerId:playerIdList) {
			PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(aPlayerId);
			long jindou = poc.getJindou();
			respMsg.hall_info.members[i++] = new SSSMemberInfo(aPlayerId, poc.getHeadimg(), poc.getLevel(),
					poc.getSex().shortValue(),(int)jindou, poc.getName());
		}
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		
		
	}

}
